package com.zrwl.poscloud.xo.utils;

import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.vo.ReportDetailVO;
import com.zrwl.poscloud.xo.vo.ReportMainVO;

import java.util.Iterator;
import java.util.Map;

/**
 * @author 刘思童
 * @create 2022-09-13 20:23
 **/
public class BuildSqlForReportSelect {
    public static void getSqlPlus(ReportMainVO reportMainVO) {
        String sql = getSql(reportMainVO.getMainSelectConfig(), JSONObject.parseObject(JSONObject.toJSONString(reportMainVO)));
        reportMainVO.setSelectSqlPlus(sql);
    }

    public static void getSqlPlus(ReportDetailVO reportDetailVO) {
        String sql = getSql(reportDetailVO.getDetailSelectConfig(), JSONObject.parseObject(JSONObject.toJSONString(reportDetailVO)));
        reportDetailVO.setSelectSqlPlus(sql);
    }

    static String getSql(String selectConfigStr, JSONObject jsonObject) {
        String sql = "";
        JSONObject selectConfig = JSONObject.parseObject(selectConfigStr);
        if (selectConfig == null) return sql;
        Iterator iter = selectConfig.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            JSONObject config = (JSONObject) entry.getValue();
            if (StringUtils.isNotEmpty(config.getString("s"))) {
                String value = jsonObject.getString(entry.getKey().toString());
                if (StringUtils.isNotEmpty(value)) {
                    String keyword = entry.getKey().toString();
                    sql += (" and " + keyword.replace("Keyword", "_keyword") + getLogical(config.getIntValue("w"), keyword, "param"));
                }
            }
        }
        return sql;
    }

    public static void getSqlPlusForBuild(ReportMainVO reportMainVO) {
        String sql = getSqlForBuild(reportMainVO.getMainSelectConfig(), JSONObject.parseObject(JSONObject.toJSONString(reportMainVO)));
        reportMainVO.setSelectSqlPlus(sql);
    }

    public static void getSqlPlusForBuild(ReportDetailVO reportDetailVO) {
        String sql = getSqlForBuild(reportDetailVO.getDetailSelectConfig(), JSONObject.parseObject(JSONObject.toJSONString(reportDetailVO)));
        reportDetailVO.setSelectSqlPlus(sql);
    }

    static String getSqlForBuild(String selectConfigStr, JSONObject jsonObject) {
        String sql = "where 1=1";
        JSONObject selectConfig = JSONObject.parseObject(selectConfigStr);
        if (selectConfig == null) return "";
        Iterator iter = selectConfig.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            JSONObject config = (JSONObject) entry.getValue();
            if (StringUtils.isNotEmpty(config.getString("s"))) {
                String value = jsonObject.getString(entry.getKey().toString());
                if (StringUtils.isNotEmpty(value)) {
                    String keyword = config.getString("s");
                    sql += (" and " + keyword + getLogical(config.getIntValue("w"), keyword, null));
                }
            }
        }
        return sql;
    }

    static String getLogical(int type, String keyword, String paramName) {
        String paramNameFull = (paramName == null ? "" : (paramName + ".")) + keyword;
        switch (type) {
            case 1:
                return "= #{" + paramNameFull + "}";
            case 2:
                return "(name like concat (#{" + paramNameFull + "},'%'))";
            case 3:
                return "(name like concat ('%',#{" + paramNameFull + "}))";
            case 4:
                return "(name like concat ('%',#{" + paramNameFull + "},'%'))";
            case 5:
                return "> #{" + paramNameFull + "}";
            case 6:
                return ">= #{" + paramNameFull + "}";
            case 7:
                return "< #{" + paramNameFull + "}";
            case 8:
                return "<= #{" + paramNameFull + "}";
            default:
                return "= #{" + paramNameFull + "}";
        }
    }

    public static void setReportMainVOToMap(Map param, ReportMainVO vo) {
        JSONObject selectConfig = JSONObject.parseObject(vo.getMainSelectConfig());
        if (StringUtils.isNotEmpty(vo.getLongKeyword1())) {
            param.put(selectConfig.getJSONObject("longKeyword1").getString("s"), vo.getLongKeyword1());
        }
        if (StringUtils.isNotEmpty(vo.getLongKeyword2())) {
            param.put(selectConfig.getJSONObject("longKeyword2").getString("s"), vo.getLongKeyword2());
        }
        if (StringUtils.isNotEmpty(vo.getLongKeyword3())) {
            param.put(selectConfig.getJSONObject("longKeyword3").getString("s"), vo.getLongKeyword3());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword1())) {
            param.put(selectConfig.getJSONObject("shortKeyword1").getString("s"), vo.getShortKeyword1());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword2())) {
            param.put(selectConfig.getJSONObject("shortKeyword2").getString("s"), vo.getShortKeyword2());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword3())) {
            param.put(selectConfig.getJSONObject("shortKeyword3").getString("s"), vo.getShortKeyword3());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword4())) {
            param.put(selectConfig.getJSONObject("shortKeyword4").getString("s"), vo.getShortKeyword4());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword1())) {
            param.put(selectConfig.getJSONObject("shortKeyword5").getString("s"), vo.getShortKeyword5());
        }
        if (vo.getNumberKeyword1() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword1").getString("s"), vo.getNumberKeyword1());
        }
        if (vo.getNumberKeyword2() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword2").getString("s"), vo.getNumberKeyword2());
        }
        if (vo.getNumberKeyword3() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword3").getString("s"), vo.getNumberKeyword3());
        }
        if (vo.getNumberKeyword4() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword4").getString("s"), vo.getNumberKeyword4());
        }
        if (vo.getNumberKeyword5() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword5").getString("s"), vo.getNumberKeyword5());
        }
        param.put("selectSqlPlus", vo.getSelectSqlPlus());
    }

    static void setReportDetailVOToMap(Map param, ReportDetailVO vo) {
        JSONObject selectConfig = JSONObject.parseObject(vo.getDetailSelectConfig());
        if (StringUtils.isNotEmpty(vo.getLongKeyword1())) {
            param.put(selectConfig.getJSONObject("longKeyword1").getString("s"), vo.getLongKeyword1());
        }
        if (StringUtils.isNotEmpty(vo.getLongKeyword2())) {
            param.put(selectConfig.getJSONObject("longKeyword2").getString("s"), vo.getLongKeyword2());
        }
        if (StringUtils.isNotEmpty(vo.getLongKeyword3())) {
            param.put(selectConfig.getJSONObject("longKeyword3").getString("s"), vo.getLongKeyword3());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword1())) {
            param.put(selectConfig.getJSONObject("shortKeyword1").getString("s"), vo.getShortKeyword1());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword2())) {
            param.put(selectConfig.getJSONObject("shortKeyword2").getString("s"), vo.getShortKeyword2());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword3())) {
            param.put(selectConfig.getJSONObject("shortKeyword3").getString("s"), vo.getShortKeyword3());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword4())) {
            param.put(selectConfig.getJSONObject("shortKeyword4").getString("s"), vo.getShortKeyword4());
        }
        if (StringUtils.isNotEmpty(vo.getShortKeyword1())) {
            param.put(selectConfig.getJSONObject("shortKeyword5").getString("s"), vo.getShortKeyword5());
        }
        if (vo.getNumberKeyword1() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword1").getString("s"), vo.getNumberKeyword1());
        }
        if (vo.getNumberKeyword2() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword2").getString("s"), vo.getNumberKeyword2());
        }
        if (vo.getNumberKeyword3() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword3").getString("s"), vo.getNumberKeyword3());
        }
        if (vo.getNumberKeyword4() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword4").getString("s"), vo.getNumberKeyword4());
        }
        if (vo.getNumberKeyword5() != null) {
            param.put(selectConfig.getJSONObject("numberKeyword5").getString("s"), vo.getNumberKeyword5());
        }
        param.put("selectSqlPlus", vo.getSelectSqlPlus());
    }
}
